package com.burgess.net.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 持久型消费者
 * 创建人    BurgessLee
 * 创建时间   2020/5/20
 * 描述
 */
public class PersistantMessageConsumer {

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = activeMQConnectionFactory.createConnection();

        //设置消费者ID，用来识别消费者
        connection.setClientID("cc1");

        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Topic destination = session.createTopic("my-persistant-topic");
        TopicSubscriber ts = session.createDurableSubscriber(destination, "T1");

        connection.start();

        TextMessage textMessage = (TextMessage) ts.receive();
        while(textMessage != null){
            System.out.println("message: " + textMessage.getText());
            session.commit();
            textMessage = (TextMessage) ts.receive(1000L);
        }

        session.close();
        connection.close();


    }
}
